#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <float.h>

/**
 * Convert a double matrix into a single matrix, optionally transposing.
 */
void double2float(float *output, const double *input,
		unsigned int size_x, unsigned int size_y, int transpose)
{
	unsigned int x, y;

	if (transpose)
	{
		for (y = 0; y < size_y; y++)
		{
			for (x = 0; x < size_x; x++)
			{
				output[y*size_x + x] = (float)input[y + x*size_y];
			}
		}
	}
	else
	{
		for (y = 0; y < size_y; y++)
		{
			for (x = 0; x < size_x; x++)
			{
				output[y*size_x + x] = (float)input[y*size_x + x];
			}
		}
	}
}

/**
 * Convert a single matrix into a double matrix, optionally transposing.
 */
void float2double(double *output, const float *input,
		unsigned int size_x, unsigned int size_y, int transpose)
{
	unsigned int x, y;

	if (transpose)
	{
		for (y = 0; y < size_x; y++)
		{
			for (x = 0; x < size_y; x++)
			{
				output[y*size_y + x] = (double)input[y + x*size_x];
			}
		}
	}
	else
	{
		for (y = 0; y < size_y; y++)
		{
			for (x = 0; x < size_x; x++)
			{
				output[y*size_x + x] = (double)input[y*size_x + x];
			}
		}
	}
}

/**
 * Convert a single matrix into a double matrix, using the lower triangle.
 */
void float2double_lower_triangle(double *output, const float *input,
		unsigned int size_x, unsigned int size_y)
{
	unsigned int x, y;
	unsigned int counter = 0;

	for (y = 0; y < size_y; y++)
	{
		for (x = y+1; x < size_x; x++)
		{
			output[counter] = (double)input[y*size_x + x];
			counter++;
		}
	}
}
